using System;
using System.Collections.Generic;
using NUnit.Framework.Internal;
using UnityEditor;
using UnityEngine;

namespace Gj
{
    [CustomPropertyDrawer(typeof(SelectRef))]
    public class SelectRefDrawer: PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent content)
        {
            string type = ((SelectRef)attribute).type;
            string value = "";
            string value1 = "";
            bool isLink = false;
            switch (property.propertyType)
            {
                case SerializedPropertyType.String:
                    value = property.stringValue;
                    break;
                case SerializedPropertyType.Integer:
                    value = property.intValue.ToString();
                    break;
                case SerializedPropertyType.Generic:
                    value = property.FindPropertyRelative("type").stringValue;
                    value1 = property.FindPropertyRelative("key").stringValue;
                    isLink = true;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            string[] list = RefData.GetRefList(type);
            string[] _valueList = new string[list.Length + 1];
            string[] _labelList = new string[list.Length + 1];
            _valueList[0] = "-1";
            _labelList[0] = "请选择...";
            int index = 0;
            for (int i = 0; i < list.Length ; i ++)
            {
                string[] _list = list[i].Split('-');
                string _value = _list[0];
                string _label = _list.Length > 1 ? _list[1] : _list[0];
                _valueList[i+1] = _value;
                _labelList[i+1] = _label;
                if (value.Equals(_value))
                {
                    index = i+1;
                }
            }
            
            string[] list1 = !StringTools.IsEmpty(value) ? RefData.GetRefList(value) : new string[]{};
            string[] _valueList1 = new string[list1.Length + 1];
            string[] _labelList1 = new string[list1.Length + 1];
            _valueList1[0] = "-1";
            _labelList1[0] = "请选择...";
            int index1 = 0;
            for (int i = 0; i < list1.Length ; i ++)
            {
                string[] _list1 = list1[i].Split('-');
                string _value1 = _list1[0];
                string _label1 = _list1.Length > 1 ? _list1[1] : _list1[0];
                _valueList1[i+1] = _value1;
                _labelList1[i+1] = _label1;
                if (value1.Equals(_value1))
                {
                    index1 = i+1;
                }
            }
            
            EditorGUI.BeginChangeCheck();
            
            if (isLink)
            {
                content = EditorGUI.BeginProperty(position, content, property);
                var contentPosition = EditorGUI.PrefixLabel(position, content);
                EditorGUIUtility.labelWidth = 1f;
                contentPosition.width /= 2f;
                index = EditorGUI.Popup(contentPosition, index, _labelList);
                contentPosition.x += contentPosition.width;
                index1 = EditorGUI.Popup(contentPosition, index1, _labelList1);
            }
            else
            {
                EditorGUI.BeginProperty(position, GUIContent.none, property);
                index = EditorGUI.Popup(position, content.text, index, _labelList);
            }

            if (EditorGUI.EndChangeCheck())
            {
                switch (property.propertyType)
                {
                    case SerializedPropertyType.String:
                        property.stringValue = _valueList[index];
                        break;
                    case SerializedPropertyType.Integer:
                        property.intValue = Convert.ToInt32(_valueList[index]);
                        break;
                    case SerializedPropertyType.Generic:
                        property.FindPropertyRelative("type").stringValue = _valueList[index];
                        property.FindPropertyRelative("key").stringValue = _valueList1[index1];
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }

            EditorGUI.EndProperty();
        } 
    }
}